using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Prime31;
using UnityEngine;

public class SocialManager
{
	public enum SharePlatform
	{
		Facebook = 0,
		Twitter = 1,
		Other = 2
	}

	public delegate void ShareResultEventhandler(SharePlatform platform, bool success);

	private const string fbPostMesageRewarded = "Hey, check out this game, GyroSphere Trials!";

	private const string twitterPostMessageRewarded = "Hey, check out this game, GyroSphere Trials!";

	private const string otherPostMessageRewarded = "Hey, check out this game, GyroSphere Trials!";

	private const string fullLink = "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride";

	private const string fullLinkRewarded = "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride";

	private const string defaultShareImagePath = "share.png";

	private string fbPostMesage
	{
		get
		{
			return "Check out my score on GyroSphere Trials!";
		}
	}

	private string twitterPostMessage
	{
		get
		{
			return "Check out my score on GyroSphere Trials!";
		}
	}

	private string otherPostMessage
	{
		get
		{
			return "Check out my score on GyroSphere Trials!";
		}
	}

	public bool canUseFB
	{
		get
		{
			if (Application.internetReachability == NetworkReachability.NotReachable)
			{
				return false;
			}
			Debug.Log("CustomShareWithIntent.isFacebookInstalled " + CustomShareWithIntent.isFacebookInstalled);
			return CustomShareWithIntent.isFacebookInstalled;
		}
	}

	public bool canUseTwitter
	{
		get
		{
			if (Application.internetReachability == NetworkReachability.NotReachable)
			{
				return false;
			}
			Debug.Log("CustomShareWithIntent.isTwitterInstalled " + CustomShareWithIntent.isTwitterInstalled);
			return CustomShareWithIntent.isTwitterInstalled;
		}
	}

	[method: MethodImpl(32)]
	public event ShareResultEventhandler ShareResultEvent;

	private string GetTimeFormatted()
	{
		int minutes;
		int seconds;
		int hundreds;
		Utils.GetSplitedTime(Game.levelTime, out minutes, out seconds, out hundreds);
		return minutes.ToString("D2") + ":" + seconds.ToString("D2") + "." + hundreds.ToString("D2");
	}

	public void Share(SharePlatform platform, bool rewarded, string imagePath = "")
	{
		switch (platform)
		{
		case SharePlatform.Facebook:
			ShareToFB(rewarded, imagePath);
			break;
		case SharePlatform.Twitter:
			ShareToTwitter(rewarded, imagePath);
			break;
		case SharePlatform.Other:
			ShareToOther(rewarded, imagePath);
			break;
		}
	}

	public void ShareToFB(bool rewarded, string imagePath = "")
	{
		if (!canUseFB)
		{
			Debug.Log("can;t show composer");
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Facebook, false);
			}
		}
		else if (rewarded)
		{
			Globals.appBackgroundReason = AppBackgroundReason.Facebook;
			CustomShareWithIntent.ShareToFacebook("Hey, check out this game, GyroSphere Trials!", "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", null);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Facebook, true);
			}
		}
		else if (imagePath != string.Empty)
		{
			Globals.appBackgroundReason = AppBackgroundReason.Facebook;
			CustomShareWithIntent.ShareToFacebook(fbPostMesage, "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", imagePath);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Facebook, true);
			}
		}
	}

	private void FBComposerResult(bool success)
	{
		Debug.Log("FB Composer success : " + success);
		Globals.appBackgroundReason = AppBackgroundReason.Unknown;
		if (this.ShareResultEvent != null)
		{
			this.ShareResultEvent(SharePlatform.Facebook, success);
		}
	}

	public void ShareToTwitter(bool rewarded, string imagePath = "")
	{
		if (rewarded)
		{
			Globals.appBackgroundReason = AppBackgroundReason.Twitter;
			CustomShareWithIntent.ShareToTwitter("Hey, check out this game, GyroSphere Trials!", "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", null);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Twitter, true);
			}
		}
		else if (imagePath != string.Empty)
		{
			Globals.appBackgroundReason = AppBackgroundReason.Twitter;
			CustomShareWithIntent.ShareToTwitter(twitterPostMessage, "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", imagePath);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Twitter, true);
			}
		}
	}

	private void TwiterComposerResult(bool success)
	{
		Debug.Log("Twitter Composer success : " + success);
		Globals.appBackgroundReason = AppBackgroundReason.Unknown;
		if (this.ShareResultEvent != null)
		{
			this.ShareResultEvent(SharePlatform.Twitter, success);
		}
	}

	private void ShareToOther(bool rewarded, string imagePath = "")
	{
		if (rewarded)
		{
			Globals.appBackgroundReason = AppBackgroundReason.SocialOther;
			CustomShareWithIntent.ShareToOther("Hey, check out this game, GyroSphere Trials!", "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", null);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Other, true);
			}
		}
		else if (imagePath != null)
		{
			Globals.appBackgroundReason = AppBackgroundReason.SocialOther;
			CustomShareWithIntent.ShareToOther(otherPostMessage, "https://play.google.com/store/apps/details?id=com.crocostudio.ironballride", imagePath);
			Globals.appBackgroundReason = AppBackgroundReason.Unknown;
			if (this.ShareResultEvent != null)
			{
				this.ShareResultEvent(SharePlatform.Other, true);
			}
		}
	}

	private void FacebookShareDialogSuccess(Dictionary<string, object> dict)
	{
		Globals.appBackgroundReason = AppBackgroundReason.Unknown;
		Debug.Log("FacebookShareDialogSuccess");
		Prime31.Utils.logObject(dict);
		if (this.ShareResultEvent != null)
		{
			this.ShareResultEvent(SharePlatform.Facebook, true);
		}
	}

	private void FacebookShareDialogFailed(P31Error error)
	{
		Globals.appBackgroundReason = AppBackgroundReason.Unknown;
		if (this.ShareResultEvent != null)
		{
			this.ShareResultEvent(SharePlatform.Facebook, false);
		}
	}
}
